package com.bootdo.wx.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.domain.GoodsUpgradeDO;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.GoodsUpgradeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 9.9商品列表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-07-01 16:11:15
 */
 
@Controller
@RequestMapping("/wx/goodsUpgrade")
public class GoodsUpgradeController {
	@Autowired
	private GoodsUpgradeService goodsUpgradeService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsUpgrade:goodsUpgrade")
	String GoodsUpgrade(){
	    return "wx/goodsUpgrade/goodsUpgrade";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsUpgrade:goodsUpgrade")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<Map<String,Object>> goodsUpgradeList = goodsUpgradeService.getUpgradeList(query);
		int total = goodsUpgradeService.getUpgradeListCount(query);
		PageUtils pageUtils = new PageUtils(goodsUpgradeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:goodsUpgrade:add")
	String add(){
	    return "wx/goodsUpgrade/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsUpgrade:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsUpgradeDO goodsUpgrade = goodsUpgradeService.get(id);
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> list = goodsGoodsService.queryAllForIdAndName(map);
		String goodsId = goodsUpgrade.getGoodsId();
		map.put("goodsId",goodsId);
		Map<String,Object> oldMap = goodsGoodsService.queryAllForIdAndName(map).get(0);
		model.addAttribute("goodsList", list);
		model.addAttribute("goodsUpgrade", goodsUpgrade);
		model.addAttribute("oldMap", oldMap);
	    return "wx/goodsUpgrade/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsUpgrade:add")
	public R save( GoodsUpgradeDO goodsUpgrade){
		Date date = new Date();
		goodsUpgrade.setId(UuidUtil.get32UUID());
		goodsUpgrade.setDelFlag(0);
		goodsUpgrade.setCreateAt(date);
		goodsUpgrade.setUpdateAt(date);
		goodsUpgrade.setNumSale(0);
		if(goodsUpgradeService.save(goodsUpgrade)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsUpgrade:edit")
	public R update( GoodsUpgradeDO goodsUpgrade){
		goodsUpgrade.setUpdateAt(new Date());
		goodsUpgradeService.update(goodsUpgrade);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsUpgrade:remove")
	public R remove( String id){
		if(goodsUpgradeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsUpgrade:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		goodsUpgradeService.batchRemove(ids);
		return R.ok();
	}
	
}
